#include"MainWindow.h" 

#include"Log.h"
#include"Map.h"
#include"MapDisplay.h"
#include"TurnSequenceWidget.h"

MainWindow::MainWindow(){
	m_outerLayout = new QHBoxLayout(this);
	setLayout(m_outerLayout);
	
	m_log = Log::instance();
	m_log->setParent(this);
	m_outerLayout->addWidget(m_log);
	
	m_widget = new QWidget(this);
	m_outerLayout->addWidget(m_widget);
	
	m_layout = new QVBoxLayout(m_widget);
	m_widget->setLayout(m_layout);
	
	m_buttonWidget = new QWidget(m_widget);
	m_buttonLayout = new QHBoxLayout(m_buttonWidget);
	m_buttonWidget->setLayout(m_buttonLayout);
	m_layout->addWidget(m_buttonWidget);
	
	m_turnButton = new QPushButton("Do Turn!", m_buttonWidget);
	connect(m_turnButton, SIGNAL(clicked()), this, SLOT(slotDoTurn()));
	m_buttonLayout->addWidget(m_turnButton);
	
	m_distanceButton = new QPushButton("Show distances", m_buttonWidget);
	connect(m_distanceButton, SIGNAL(clicked()), this, SLOT(slotShowDistances()));
	m_buttonLayout->addWidget(m_distanceButton);
	
	m_display = 0;
	m_turnSequence = 0;
}

MainWindow::~MainWindow(){
	
}

void MainWindow::setMapDisplay(MapDisplay *display){
	m_display = display;
	display->setParent(m_widget);
	m_layout->addWidget(display);
	
	m_turnSequence = new TurnSequenceWidget(display->map()->turnSequence(), this);
	m_outerLayout->addWidget(m_turnSequence);
	connect(display->map(), SIGNAL(turnEnded(Unit*)), m_turnSequence, SLOT(slotTurnEnded(Unit*)));
	connect(display->map(), SIGNAL(unitDamaged(Unit*)), m_turnSequence, SLOT(slotUnitDamaged(Unit*)));
	connect(display->map(), SIGNAL(unitDied(Unit*)), m_turnSequence, SLOT(slotUnitDied(Unit*)));
}

void MainWindow::slotDoTurn(){
	if(!m_display->doTurn()){
		m_turnButton->setEnabled(false);
	}
}

void MainWindow::slotShowDistances(){
	m_distanceButton->setText("Hide distances");
	m_distanceButton->disconnect();
	connect(m_distanceButton, SIGNAL(clicked()), this, SLOT(slotHideDistances()));
	m_display->showDistances();
}

void MainWindow::slotHideDistances(){
	m_distanceButton->setText("Show distances");
	m_distanceButton->disconnect();
	connect(m_distanceButton, SIGNAL(clicked()), this, SLOT(slotShowDistances()));
	m_display->hideDistances();
}
